/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @file softservo.cpp
* 
* Library for easy managing the power of an engine.
* 
* @author Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "softservo.hpp"

CMSoftServo::CMSoftServo(int acceleration=1,int min=-100,int max=100)
{
	this->now=this->end=0;
	this->acceleration = acceleration;
	this->min = min;
	this->max = max;
}

CMSoftServo::~CMSoftServo()
{}

void CMSoftServo::setEnd(int value)
{
	if(value > this->max){ this->end = this->max; }
	else if(value < this->min){ this->end = this->min; }
	else { this->end = value; }
}

int CMSoftServo::getNow()
{
	return this->now;
}

void CMSoftServo::stop()
{
	this->now = 0;
	this->end = 0;
}

int CMSoftServo::update()
{
	if(this->now == this->end)
	{
		return 0;
	}
	// Forward
	if(this->now < this->end)
	{
		this->now += this->acceleration;
		if(this->now > this->end){ this->now = this->end; }
	}
	//Backward
	if(this->now > this->end)
	{
		this->now -= this->acceleration;
		if(this->now < this->end){ this->now = this->end; }	
	}
}
